mod args;
mod logger;

use anyhow::Result;
use clap::Parser;
use std::sync::Arc;
use tracing::{error, info};

use rmcp::{ServiceExt, transport::stdio};
use rmcp_gd::gd_handler::GdHandler;
use rmcp_gd::gd_manager::GdManager;

#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
    let args = args::Args::parse();

    logger::init(args.log_level);

    info!("====================[RMCP GD STDIO]====================");

    // Create godot manager
    let gd_manager = GdManager::new(&args.ws_url).map_err(|e| {
        error!("[stdio] Failed to create database manager: {}", e);
        anyhow::Error::msg(e.to_string())
    })?;

    // Create godot handler
    let handler = GdHandler::new(Arc::new(gd_manager));

    info!("[stdio] Starting RMCP GD Server...");

    // Start server
    let service = handler.serve(stdio()).await.inspect_err(|e| {
        error!("[stdio] Server startup failed: {:?}", e);
    })?;

    service.waiting().await?;

    Ok(())
}
